package es.udc.fbellas.j2ee.tiendaweb.model.categoria.DAO;

import java.sql.Connection;

import javax.sql.DataSource;

import es.udc.fbellas.j2ee.tiendaweb.model.categoria.TO.CategoriaTO;
import es.udc.fbellas.j2ee.util.configuration.ConfigurationParametersManager;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;

/**
 * A factory to get <code>SQLAccountDAO</code> objects.
 * <p>
 * Required configuration parameters:
 * <ul>
 * <li><code>SQLAccountDAOFactory/daoClassName</code>: it must specify the full
 * class name of the class implementing <code>SQLAccountDAO</code>.</li>
 * </ul>
 */
public class SQLCategoriaDAOFactory {

    private final static String DAO_CLASS_NAME_PARAMETER =
        "SQLCategoriaDAOFactory/daoClassName";

    private final static Class daoClass = getDAOClass();
    
    private SQLCategoriaDAOFactory() {}
    
    private static Class getDAOClass() {
    
        Class theClass = null;
    
        try {
        
            String daoClassName = 
                ConfigurationParametersManager.getParameter(
                    DAO_CLASS_NAME_PARAMETER);
            
            theClass = Class.forName(daoClassName);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return theClass;
        
    }
    
    public static SQLCategoriaDAO getDAO() throws InternalErrorException {
        
        try {        
            return (SQLCategoriaDAO) daoClass.newInstance();
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
        
    }
    
    /* 
     * Test code. Uncomment for testing. Test this class and any class
     * implementing "SQLAccountDAO".
     */         
    public static void main (String[] args) {    
        
        Connection connection = null;
    
        try {        
            
            /* Get a connection. */
            DataSource dataSource = new SimpleDataSource();
            connection = dataSource.getConnection();        
                    
//            /* Get dao. */
            SQLCategoriaDAO dao = SQLCategoriaDAOFactory.getDAO();
//
//            /* Test "SQLCategoriaDAO::create". */
//            Long userIdentifier = new Long(76412005);
//            System.out.println("Test for 'SQLCategoriaDAO::create'");
            CategoriaTO categoriaTO1 = new CategoriaTO(21342, "LIBROS");
//            accountVO1 = dao.create(connection, accountVO1);
//            System.out.println("accountVO1 created => " + accountVO1);
//            
//            /* Test "SQLAccountDAO::exists". */
//            System.out.println("Test for 'SQLAccountDAO::exists'");
//            if (!dao.exists(connection, accountVO1.getAccountIdentifier())) {
//                throw new Exception("Can not find account with " +
//                    "accountIdentifier = " + 
//                    accountVO1.getAccountIdentifier());
//            }
//            
            /* Test "SQLAccountDAO::find". */
            System.out.println("Test for 'SQLCategoriaDAO::find'");
            System.out.println(dao.find(connection,
                categoriaTO1.getIdCategoria()));
            
            /* Test "SQLAccountDAO::find". */
            System.out.println("Test for 'SQLCategoriaDAO::findByNombre'");
            System.out.println(dao.findByNombre(connection,
                categoriaTO1.getNombre()));

            
//            /* Test "SQLAccountDAO::findByUserIdentifier". */
//            System.out.println("Test for " +
//                "'SQLAccountDAO::findByUserIdentifier'");
//            for (int i=1; i<21; i++) {
//                AccountVO accountVO = new AccountVO(new Long(-1), 
//                    userIdentifier, 1000+i);
//                accountVO =    dao.create(connection, accountVO);
//                System.out.println("accountVO added => " + accountVO);
//            }
//            
//            /* Get value objects in groups of 10. */            
//            int startIndex = 1;
//            int groupCount = 10;
//            List accountVOs;
//            
//            do {
//            
//                accountVOs = dao.findByUserIdentifier(connection,
//                    userIdentifier, startIndex, groupCount);
//                
//                /* Print value objects. */    
//                Iterator accountVOIterator = accountVOs.iterator();
//                while (accountVOIterator.hasNext()) {
//                    AccountVO accountVO = (AccountVO) accountVOIterator.next();
//                    System.out.println(accountVO);
//                }
//                System.out.println("----------------------------------------");
//                
//                startIndex += groupCount;
//                
//            } while (accountVOs.size() == groupCount);
//            
//            /* Test "SQLAccountDAO::update". */
//            System.out.println("Test for 'SQLAccountDAO::update'");
//            accountVO1.setBalance(accountVO1.getBalance()+999);
//            dao.update(connection, accountVO1);
//            System.out.println(dao.find(connection,
//                accountVO1.getAccountIdentifier()));
//                
//            /* Test "SQLAccountDAO::remove". */
//            System.out.println("Test for 'SQLAccountDAO::remove'");
//            dao.remove(connection, accountVO1.getAccountIdentifier());
//            
//            if (dao.exists(connection, accountVO1.getAccountIdentifier())) {
//                throw new Exception("SQLAccountDAO::remove failed");
//            }
//                
//            /* Tests OK. */
//            System.out.println("Tests OK !!!!");
//        
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                GeneralOperations.closeConnection(connection);
            } catch (InternalErrorException e) {
                e.printStackTrace();
            }
        }
                
    }    
    
}
